<template>
  <div>
    <div style="margin-bottom: 20px;margin-top: 20px;">
      <!-- <el-button type="primary" @click="addRef.isShow = true">添加信息</el-button> -->
    </div>

    <el-form label-width="80px" :inline="true">
      <el-form-item label="角色名称">
        <el-input v-model="queryData.name"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryData.status">
          <el-option v-for="(item, index) in stateList" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addRef.isShow = true" style="margin-left:300px;">添加信息</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="list" :stripe="true" :border="true" :fit="true" v-if="list.length > 0">
      <!-- 自定义表格列 -->
      <template v-for="item in columns" :key="item.prop">
        <el-table-column :label="item.label" :prop="item.prop">
          <template #default="{ row }">
            {{ item.render ? item.render(row) : row[item.prop] }}
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button @click="editRow(row.id)" type="success">修改</el-button>
          <el-popconfirm title="确认删除吗？" @confirm="delRow(row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <Wait v-else></Wait>
    <el-pagination @size-change="sizeChange" @current-change="currentChange" v-model:current-page="queryData.current"
      :page-sizes="[3, 5, 10]" v-model:page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="count" background>
    </el-pagination>
    <addForm ref="addRef" :userInfo="userInfo" :props="props" @updateList=" getList(ROLETAB, queryData)" />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useListAsync } from '../../../xf-hook/getList.js'
import { ROLETAB } from '../../../urlConst/getUrl.js'
import { useChangePage } from '../../../xf-hook/page.js'
import addForm from './addForm.vue'
import { menuApi, delRoleInfoApi } from '../../../api/auth.js'
import { transListToTreeData } from '../../../utils/tree'
import { ElMessage } from "element-plus";
import { useSeekState } from '../../../xf-hook/index'
import Wait from '../../../components/Xf/Wait.vue'
const stateList = useSeekState()
const addRef = ref();
const queryData = reactive({
  create_time: null,
  current: 1,
  pageSize: 10,
  name: '',
  status: ''
})
const props = {
  children: 'children',
  label: 'title',
}
const userInfo = ref([])
const getUserInfo = async () => {
  await menuApi().then((res) => {
    userInfo.value = transListToTreeData(res.data, 0)
  })
}
const { getList, list, count } = useListAsync()
const pageChange = useChangePage()
const sizeChange = (val) => {
  pageChange.sizeChange(val, queryData.pageSize)
  getList(ROLETAB, queryData)
}
const currentChange = (val) => {
  pageChange.currentChange(val, queryData.current)
  getList(ROLETAB, queryData)
}
const reset = () => {
  Object.assign(queryData, {
    create_time: null,
    current: 1,
    pageSize: 10,
    name: '',
    status: ''
  });
}
const onReset = () => {
  reset()
  getList(ROLETAB, queryData)
}
const onSearch = () => {
  queryData.current = 1;
  getList(ROLETAB, queryData)
  reset()
}
onMounted(() => {
  getList(ROLETAB, queryData)
  getUserInfo()
})
const columns = [
  { prop: 'id', label: 'ID' },
  { prop: 'name', label: '角色名称' },
  { prop: 'description', label: '角色名称描述' },
  { prop: 'status', label: '状态' },
  { prop: 'create_time', label: '创建时间' },
]
const editRow = (id) => {
  addRef.value.replyData(id);
};
const delRow = async (id) => {
  let res = await delRoleInfoApi(id)
  ElMessage.success(res.msg);
  getList(ROLETAB, queryData)
}
</script>

<style scoped></style>